#include "OpDelete.h"
#include "Qt3DPicker.h"
#include "INode.h"
#include "Registry.h"
#include "ActorTable.h"

class OpDeletePrivate
{
public:
	std::vector<std::shared_ptr<INode>> m_children;
};

OpDelete::OpDelete(const std::vector<std::shared_ptr<INode>>& children, QUndoCommand *parent)
	: Op(parent)
	, d_ptr(new OpDeletePrivate)
{
	d_ptr->m_children = children;
}

OpDelete::~OpDelete()
{
	delete d_ptr;
}

#include "QtOSGWidget.h"
void OpDelete::redo()
{
	Registry &reg = Registry::instance();
	auto widget3D = reg.get<QtOSGWidget>(widget3DID);
	widget3D->pauseRenderThread();

	for (auto p : d_ptr->m_children)
	{
		auto parent = p->getParent();
		assert(parent);
		if (parent)
		{
			parent->removeChild(p);
		}
	}

	widget3D->resumeRenderThread();
}

void OpDelete::undo()
{
	Registry &reg = Registry::instance();
	auto widget3D = reg.get<QtOSGWidget>(widget3DID);
	widget3D->pauseRenderThread();

	// 如果对象很多，信号会被调用很多次，导致程序长时间无响应
	widget3D->getPicker()->blockSignals(true);

	for (auto p : d_ptr->m_children)
	{
		auto parent = p->getParent();
		assert(parent);
		if (parent)
		{
			parent->addChild(p);
		}
	}

	// 发出一次消息
	widget3D->getPicker()->blockSignals(false);
	widget3D->getPicker()->userFeedback();

	widget3D->resumeRenderThread();
}
